<template>
  <el-form ref="settingForm" :loading="loading" :model="setting" 
    label-position="right" label-width="90px">
    <el-col :span="8">
      <el-form-item prop="autoDispatch" label="自动出票:">
        <el-switch v-model="setting.autoDispatch" on-color="#13ce66" off-color="#ff4949" />
      </el-form-item>
      <el-form-item prop="downloadReceipt" label="下载收据:">
        <el-switch v-model="setting.downloadReceipt" on-color="#13ce66" off-color="#ff4949" />
      </el-form-item>
      <el-form-item prop="lang" label="国际化:">
        <el-select v-model="setting.lang" placeholder="选择国际化语言">
          <el-option v-for="(value, key) in Lang" :key="key" :label="value" :value="key"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="profit" label="额外利润:">
        <el-input v-model="setting.profit" placeholder="万分比"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="width:100%;" :loading="setting.updating" @click="update()">更新设置</el-button>
      </el-form-item>
    </el-col>
  </el-form>
</template>

<script>
import { Dtb } from '@/store/api'
import * as Constants from '@/constants'

export default {
  name: 'dtb-account',
  props: {
    dtbId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      ...Constants,
      loading: true,
      setting: {
        updating: false
      }
    }
  },
  created() {
    this.loading = true
    Dtb.getSetting(this.dtbId).then(setting => {
      this.setting = setting
      this.loading = false
    })
  },
  methods: {
    update() {
      this.$refs['settingForm'].validate(valid => {
        if (valid) {
          this.setting.updating = true
          var form = Object.assign({}, this.setting)
          Dtb.updateSetting(form).then(data => {
            this.setting = Object.assign({}, this.setting, data)
            this.$message.success('分销商设置已更新')
            this.setting.updating = false
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>
